package com.ly.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.ly.bean.EasybuyUser;
import com.ly.bean.EasybuyUserAddress;
import com.ly.bean.Result;
import com.ly.bean.UserVip;
import com.ly.mapper.EasybuyUserAddressMapper;
import com.ly.mapper.EasybuyUserMapper;

import com.ly.mapper.UserVipMapper;
import com.ly.service.UserService;
import com.ly.util.JsonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author 李岩
 * @date 2021/12/18-9:18
 * @describe
 */
@Component
@Service(interfaceClass = UserService.class)
public class UserServiceImpl implements UserService {
    private final Logger logger= LoggerFactory.getLogger(UserServiceImpl.class);
    @Autowired
    private EasybuyUserMapper userMapper;
    private UserServiceImpl(){
        System.out.println("进入了UserServiceImpl  service层");
    }
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Autowired
    private EasybuyUserAddressMapper userAddressMapper;
    @Autowired
    private UserVipMapper userVipMapper;
    /**
     * 根据用户名查询用户信息 登录验证
     */
    @Override
    public EasybuyUser selectUserByName(String loginname) throws Exception {
        EasybuyUser loginUser = userMapper.selectUserByName(loginname);
        return loginUser;
    }
    /**
     * 把用户对象存到redis中
     */
    @Override
    public void saveLoginUserToRedis(EasybuyUser easybuyUser, String uuid) throws Exception {
        logger.info("把用户对象存到redis中  "+easybuyUser);
        String loginStr = JsonUtil.objectToJson(easybuyUser);
        //把已登录的用户对象存到redis中
        redisTemplate.boundValueOps("loginUser_"+uuid).set(loginStr);
        //设置有效时常
        redisTemplate.boundValueOps("loginUser_" + uuid).expire(2, TimeUnit.MINUTES);
    }

    /**
     * 根据uuid从redis查询用户，验证是否登录
     */
    @Override
    public EasybuyUser findUserByRedis(String uuid) throws Exception {
        //把已登录的用户对象存到redis中
        String loginStr = redisTemplate.boundValueOps("loginUser_" + uuid).get();
        //设置有效时常
        redisTemplate.boundValueOps("loginUser_" + uuid).expire(2, TimeUnit.MINUTES);
        return JsonUtil.jsonToPojo(loginStr,EasybuyUser.class);
    }
    /**
     * 根据用户id获取用户的地址
     */
    @Override
    public List<EasybuyUserAddress> selectUserAdressByUid(Integer uid) throws Exception {
        return userAddressMapper.selectUserAdressByUid(uid);
    }

    /**
     * 注册的时候添加用户信息
     */
    @Override
    public int addUser(EasybuyUser user) throws Exception {
        logger.info("addUser"+user);
        userMapper.insertSelective(user);
        logger.info("userid "+user.getId());
        return user.getId();
    }


    /*
     * 用户激活以后修改用户的状态
     * */
    @Override
    public void updateUser(EasybuyUser user) throws Exception {
        userMapper.updateByPrimaryKey(user);
    }

    /*
        根据用户id查询用户对象
        * */
    @Override
    public EasybuyUser selectByPrimaryKey(Integer uid) throws Exception {
        return userMapper.selectByPrimaryKey(uid);
    }

    /*
     *       根据用户id来判断是否是vip
     */
    @Override
    public int findUserVipByuid(UserVip userVip) throws Exception {
        return userVipMapper.findUserVipByuid(userVip);
    }
    //添加新的收货地址
    @Override
    public void addUserAddress(EasybuyUserAddress userAddress) throws Exception {
        userAddressMapper.insertSelective(userAddress);
    }
}
